﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace rmTest.Common.ExtensionUtil
{
    public static class TypeExtensions
    {
        /// <summary>
        /// 判断一个类型是否为可空类型
        /// </summary>
        /// <param name="theType"></param>
        /// <returns></returns>
        public static bool IsNullableType(this Type theType)
        {
            return (theType.IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
        }

        /// <summary>
        /// 获取一个类型的基础类型，如果不是可为空类型就返回自己
        /// </summary>
        /// <param name="theType"></param>
        /// <returns></returns>
        public static Type GetBaseType(this Type theType)
        {
            if (theType.IsNullableType())
            {
                NullableConverter nullableConverter = new NullableConverter(theType);
                return nullableConverter.UnderlyingType;
            }
            return theType;
        }
    }
}
